package org.xshop.service.impl;

import java.io.File;
import java.util.Map;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.transaction.annotation.Transactional;
import org.xshop.bean.Member;
import org.xshop.mapper.TMemberMapper;
import org.xshop.model.TMember;
import org.xshop.service.ITMemberService;

import com.alibaba.fastjson.JSON;
import com.weibo.api.motan.config.springsupport.annotation.MotanService;

import top.ibase4j.core.base.BaseServiceImpl;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.ExceptionUtil;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.QrcodeUtil;
import top.ibase4j.core.util.UploadUtil;

/**
 * <p>
 * 会员  服务实现类
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-03-01
 */
@MotanService(interfaceClass = ITMemberService.class)
@CacheConfig(cacheNames = "TMember")
public class MemberService extends BaseServiceImpl<TMember, TMemberMapper> implements ITMemberService {
    @Override
    @Transactional
    public TMember update(TMember record) {
        if (record.getId() == null) {
            record.setUserName("会员" + record.getPhone());
            record.setNickName("会员" + record.getPhone());
        }
        TMember result = super.update(record);

        if (result != null) {
            if (result.getId() != null) {
                if (DataUtil.isEmpty(result.getQrCode())) {
                    try {
                        File file = new File("/usr/temp");
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        Map<String, Object> data = InstanceUtil.newHashMap();
                        data.put("id", result.getId());
                        data.put("type", "0");
                        String qrcodeFilePath = QrcodeUtil.createQrcode("/usr/temp", JSON.toJSONString(data));
                        String path = UploadUtil.remove2FDFS(qrcodeFilePath)
                                .getRemotePath();
                        if (DataUtil.isNotEmpty(path)) {
                            result.setQrCode(path);
                            result = super.update(result);
                        }
                    } catch (Exception e) {
                        logger.error(ExceptionUtil.getStackTraceAsString(e));
                    }
                }
            }
        }
        return result;
    }

    @Override
    public Member getBaseInfo(Long id) {
        TMember member = super.queryById(id);
        Member result = null;
        if (member != null) {
            result = InstanceUtil.to(member, Member.class);
        }
        return result;
    }

    @Override
    public TMember getInfo(Long id) {
        TMember member = super.queryById(id);
        if (member != null) {
            member.setPassword(null);
            member.setUuid(null);
        }
        return member;
    }

    @Override
    public Object updataphone(Map<String, Object> map) {
        TMember member1 = new TMember();
        member1.setIdCard((String)map.get("idCard"));
        member1.setPhone((String)map.get("orderPhone"));
        TMember member2 = super.selectOne(member1);
        if (member2.getId() == null) {
            return "请输入正确的信息 ";
        } else {
            member2.setPhone((String)map.get("newPhone"));
            return super.update(member2);
        }
    }
}